package com.shuai.shuaisecondhand_springboot.service;

import com.shuai.shuaisecondhand_springboot.entity.User;
import com.shuai.shuaisecondhand_springboot.mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    //登录
    public User login(String username, String password) {
        return userMapper.login(username, password);
    }

    //注册
    public Boolean registerUser(@Param("username") String username, @Param("password") String password) {
        try {
            // System.out.println("======>"+username+"======"+password);
            return userMapper.registerUser(username, password);
        } catch (Exception e) {
            return false;
        }

    }

    //查询
    public List<User> findAll() {
        return userMapper.findAll();
    }

    //根据用户名查找ID
    public String findId(@Param("username") String username) {
        return userMapper.findId(username);
    }

    //根据id查找所有数据
    public User findOne(@Param("id") Integer id) {
        return userMapper.findOne(id);
    }

    //插入
    public Boolean insertUser(User user) {
        try {
            return userMapper.insertUser(user);
        } catch (Exception e) {
            return false;
        }
    }

    //修改
    public Boolean updateUser(User user) {
        try {
            return userMapper.updateUser(user);
        } catch (Exception e) {
            return false;

        }

    }

    //修改个人密码
    public Boolean updatePassword(Integer Sid, String Spassword, String Snewpassword) {
        try {
            return userMapper.updatePassword(Sid, Spassword, Snewpassword);
        } catch (Exception e) {
            return false;

        }

    }

    //删除
    public Integer deleteUser(long Sid) {
        return userMapper.deleteUser(Sid);
    }

    //批量删除
    public Integer deleteUserBatch(List<Integer> Sids) {
        return userMapper.deleteUserBatch(Sids);
    }

    //分页查询
    public List<User> getUserPage(Integer pageNum, Integer pageSize, String username) {
        return userMapper.getUserPage(pageNum, pageSize, username);
    }

    //查询所有数据量
    public int getTotal(String username) {
        return userMapper.getTotal(username);
    }

    //查询用户总数
    public int getAllTotal() {
        return userMapper.getAllTotal();
    }
}
